package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.allen.library.SuperTextView;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.tongyi.lizhishop.beans.RecommendBean;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.constants.Constants;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.common.RecycleViewActivity;
import com.tongyi.lizhishop.managers.APIServiceManager;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.tongyi.lizhishop.R.id.money;


public class MyRecommendActivity extends RecycleViewActivity implements View.OnClickListener, MultiItemTypeAdapter.OnItemClickListener {


    ArrayList<Object> itemDatas = new ArrayList<>();
    private MultiItemTypeAdapter multiItemTypeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBarView(titlebar, "我的推荐");
        initView();
        refreshLayout.autoRefresh();//开启自动刷新
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void loadData() {
        APIServiceManager.getInstance().create(MessageApi.class)
                .showFriend(SPUtils.getInstance().getString("userid"), Constants.perPageSize, currentPageIndex)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<RecommendBean>() {
                    @Override
                    public void onSuccess(RecommendBean value) {
                        LogUtils.w(value);
                        if (currentPageIndex == 0) {
                            itemDatas.clear();
                        }
                        itemDatas.add(value.getSum());
                        itemDatas.addAll(value.getArray());
                        multiItemTypeAdapter.notifyDataSetChanged();
                        //这里需要传入返回的list集合而不是recycleview使用的集合
                        calculatePageIndexAndControlRefreshLayout(itemDatas);
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                        multipleStatusView.showNoNetwork();
                    }
                });

        //  multiItemTypeAdapter.notifyDataSetChanged();
    }

    private static final String TAG = "AlertMessageActivity";

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setItemAnimator(new DefaultItemAnimator());
//        recyclerview.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        multiItemTypeAdapter = new MultiItemTypeAdapter(this, itemDatas);
        multiItemTypeAdapter.addItemViewDelegate(new ItemViewDelegate() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.activity_my_recommand_recycleview_item;
            }

            @Override
            public boolean isForViewType(Object item, int position) {
                return position == 0;
            }

            @Override
            public void convert(final ViewHolder holder, final Object o, int position) {
                boolean canDraw = false;


                holder.setText(money, o.toString());
                final float sum = (float) o;
                holder.getView(R.id.withDraw).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO: 18-1-2
                        Bundle bundle = new Bundle();
                        bundle.putFloat("moneyCount", sum);
                        ActivityUtils.startActivity(bundle, MyRecommendActivity.this, WithDrawActivity.class);
                      /*  if (sum > 0) {
                            ActivityUtils.startActivity(MyRecommendActivity.this, WithDrawActivity.class);
                        } else {
                            ToastUtils.showShort("金额不足,无法提现");
                        }*/
                    }
                });
            }
        });
        multiItemTypeAdapter.addItemViewDelegate(new ItemViewDelegate() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.activity_mywallet_recommand_item;
            }

            @Override
            public boolean isForViewType(Object item, int position) {
                return position != 0;
            }

            @Override
            public void convert(ViewHolder holder, Object o, int position) {
                if (o instanceof RecommendBean.ArrayBean) {
                    RecommendBean.ArrayBean arrayBean = (RecommendBean.ArrayBean) o;
                    SuperTextView superTextView = (SuperTextView) holder.getView(R.id.superTextMyRecommend);
                    superTextView.setLeftString(arrayBean.getUser_iphone());
                    superTextView.setRightString("¥ " + arrayBean.getSum());
                }
            }
        });
        recyclerview.setAdapter(multiItemTypeAdapter);
        multiItemTypeAdapter.setOnItemClickListener(this);
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            //提现

        } else {
            Bundle bundle = new Bundle();
            RecommendBean.ArrayBean arrayBean = (RecommendBean.ArrayBean) itemDatas.get(position);
            bundle.putString("twoid", arrayBean.getUser_id());
            ActivityUtils.startActivity(bundle, this, RecommendDetailActivity.class);

        }
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
}
